'''
Created on 20/02/2012

@author: Bruna
'''
from PyQt4 import QtGui
from view.main_window import Ui_MainWindow
from view.file_chooser_dialog import Ui_FileChooserDialog

class ViewController():
    '''
    Controls the view
    '''

    def __init__(self, main_controller):
        '''
        Constructor
        
        @param main_controller: instance of the main controller
        '''
        self.main_controller = main_controller
        self.main_window = MainWindow()
        self.__fill_comboBox()
        self.__bind()
        self.__disable_tabs()
        
    def __bind(self):
        '''
        Binds signals to their slot 
        '''
        self.main_window.ui.connectionButton.clicked.connect(self.main_controller.connection)
        self.main_window.ui.speedSlider.valueChanged[int].connect(self.main_controller.change_speed)
        self.main_window.ui.clearConsoleButton.clicked.connect(self.clear_console)
        self.main_window.ui.exportConsoleButton.clicked.connect(self.export_console)        
        
    def __fill_comboBox(self):
        '''
        Fills ports and boud rate comboboxes 
        '''
        self.main_window.ui.portComboBox.addItem("COM3")
        self.main_window.ui.baudRateComboBox.addItem("9600")
        
    def __print(self, text):
        '''
        Appends a text to console
        
        @param text: text to be appended
        '''
        html = self.main_window.ui.console.toHtml()
        html = html[:-14] + "<p style='margin: 0px;'>" + text + "</p>" + html[-14:]
        self.main_window.ui.console.setHtml(html);
        self.__auto_scroll_console();
        
    def print_info(self, text):
        '''
        Appends a text to console
        
        @param text: text to be appended
        '''
        self.__print("<font color='#000000'>" + text + "</font>");
        
    def print_error(self, text):
        '''
        Appends a text to console
        
        @param text: text to be appended
        '''
        self.__print("<font color='#CC0000'>" + text + "</font>");
        
    def print_notify(self, text):
        '''
        Appends a text to console
        
        @param text: text to be appended
        '''
        self.__print("<font color='#0000CC'>" + text + "</font>");
        
    def print_warning(self, text):
        '''
        Appends a text to console
        
        @param text: text to be appended
        '''
        self.__print("<font color='#FFFF00'>" + text + "</font>");
        
    def show(self):
        '''
        Displays the view
        '''
        self.main_window.show()
        
    def clear_console(self):
        '''
        Clears console text
        '''
        self.main_window.ui.console.clear();
        
    def export_console(self):
        '''
        Opens a file chooser dialog to save console text
        '''
        content = self.main_window.ui.console.toPlainText();
        self.main_window.file_dialog.save(content);
        
    def __auto_scroll_console(self):
        '''
        Implements auto scroll for console
        '''
        cursor = self.main_window.ui.console.textCursor()
        cursor.movePosition(QtGui.QTextCursor.End)
        self.main_window.ui.console.setTextCursor(cursor)
        
    def change_connection_state(self, connected):
        '''
        Changes slider position to zero, tab state and button text
        
        @param connected: if computer application is connected with Arduino
        '''
        self.__change_connection_button(connected)
        if connected:
            self.__enable_tabs()
        else:
            self.__disable_tabs()
            
        try:
            self.main_window.ui.speedSlider.setSliderPosition(0)
        except:
            pass
        
    def __change_connection_button(self, connected):
        '''
        Changes connectionButton text
        
        @param connected: if computer application is connected with Arduino
        '''
        if connected:
            self.main_window.ui.connectionButton.setText("Disconnect")
        else:
            self.main_window.ui.connectionButton.setText("Connect")
        
    def __disable_tabs(self):
        '''
        Disables all tabs that require connection
        '''
        self.main_window.ui.tabs.setTabEnabled(1, False)
        self.main_window.ui.tabs.setTabEnabled(2, False)
        
    def __enable_tabs(self):
        '''
        Enables all tabs that require connection
        '''
        self.main_window.ui.tabs.setTabEnabled(1, True)
        self.main_window.ui.tabs.setTabEnabled(2, True)
            
class MainWindow(QtGui.QMainWindow):
    '''
    classdoc
    '''
    
    def __init__(self, parent=None):
        '''
        Constructor
        '''
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
        self.file_dialog = Ui_FileChooserDialog()
        self.file_dialog.setupUi(self)
        